﻿#ifndef GLOBALTOOLS_H
#define GLOBALTOOLS_H

#ifdef QT_DEBUG
//#define ONLY_SHOW_CHAT
#else
#endif
#include "characterset.h"

#include <QDebug>
#include <QTime>
#define log_debug() qDebug() << QTime::currentTime().toString() << __FUNCTION__

#include <string>
#include <sstream>
template<typename T>
T str2n(const std::string& str, T defaultvalue=0)
{
    T i = defaultvalue;
    if(!str.empty())
    {
        bool s = ((T)-1)<0;
        try
        {
            int size = sizeof(T);
            if(size==sizeof(int) && s)
                i = std::stoi(str);
            else if(size==sizeof(long))
            {
                if(s)
                    i = std::stol(str);
                else
                    i = std::stoul(str);
            }
            else if(size==sizeof(qlonglong))
            {
                if(s)
                    i = std::stoll(str);
                else
                    i = std::stoull(str);
            }
            else
            {
                std::istringstream ss(str);
                ss>>i;
            }
        }
        catch(...)
        {
            i = defaultvalue;
        }
    }
    return i;
}

QString cstring2QString(const char* str);
QString string2QString(const std::string& str);
QString string2QStringLocal(const std::string& str);
QByteArray QString2cstring(const QString& str);
std::string QString2string(const QString& str);

const QString appDataDir();
const QString userProfileDir();
const QString userDataDir(const QString& uid);

void openUrl(const QUrl &url);

const QByteArray create_mid(uint curtime, const QString& msg);
const QByteArray create_mid(uint curtime, const QByteArray& msg);

template<typename T>
inline void setParam(QVariantHash &container, int key, const T &value)
{
    container[QString::number(key)] = value;
}

QVariant getParam(const QVariantHash &container, int key);
bool hasParam(const QVariantHash &container, int key);
int removeParam(QVariantHash &container, int key);

template <typename T>
inline bool isOneOf(const T &value, const T &compare1, const T &compare2)
{
    return value == compare1 || value == compare2;
}
template <typename T>
inline bool isOneOf(const T &value, const T &compare1, const T &compare2, const T &compare3)
{
    return value == compare1 || value == compare2 || value == compare3;
}
template <typename T>
inline bool isOneOf(const T &value, const T &compare1, const T &compare2, const T &compare3, const T &compare4)
{
    return value == compare1 || value == compare2 || value == compare3 || value == compare4;
}

extern const int virtualCameraId;

const QString getCurrentVersion();

#ifdef Q_OS_WIN
QString winGetLastError();
#endif

QString getHost(const QString &fullAddress);
void getProxy(const QString &domain, QObject* receiver, const char *member);

#endif // GLOBALTOOLS_H
